#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mygraphicsellipseitem.h"
#include <QGraphicsRectItem>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    labelViewCord = new QLabel("View坐标：");
    labelViewCord->setMinimumWidth(150);
    labelSceneCord = new QLabel("Scene坐标");
    labelSceneCord->setMinimumWidth(150);
    labelItemCord = new QLabel("Item坐标");
    labelItemCord->setMinimumWidth(150);
    ui->statusbar->addWidget(labelViewCord);
    ui->statusbar->addWidget(labelSceneCord);
    ui->statusbar->addWidget(labelItemCord);

    ui->graphicsView->setCursor(Qt::ArrowCursor);
    ui->graphicsView->setMouseTracking(true);

    connect(ui->graphicsView, SIGNAL(mouseMovePointSignal(QPoint)),
            this, SLOT(on_mouseMovePoint(QPoint)));

    connect(ui->graphicsView, SIGNAL(mouseClickedSignal(QPoint)),
            this, SLOT(on_mouseClicked(QPoint)));

    initUI();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    ui->labelViewSize->setText(QString::asprintf("GraphicsView坐标，左上角总是(0,0)，宽度= %d，长度=%d",
                                                 ui->graphicsView->width(),
                                                 ui->graphicsView->height()));
    QRectF  rect = ui->graphicsView->sceneRect();
    ui->labelSceneRect->setText(QString::asprintf("GraphicsView::sceneRect=(%.0f, %.0f, %.0f, %.0f)",
                                                  rect.left(),
                                                  rect.right(),
                                                  rect.top(),
                                                  rect.bottom()));
}

void MainWindow::on_mouseMovePoint(QPoint point)
{
    labelViewCord->setText(QString::asprintf("View坐标：%d, %d",
                           point.x(), point.y()));

    QPointF pointScene = ui->graphicsView->mapToScene(point);
    labelSceneCord->setText(QString::asprintf("Scene坐标：%.0f, %.0f",
                           pointScene.x(), pointScene.y()));
}

void MainWindow::on_mouseClicked(QPoint point)
{
    //qDebug()<<"on_mouseClicked"<<endl;
    QPointF pointScene = ui->graphicsView->mapToScene(point);
    QGraphicsItem *item = nullptr;
    item = scene->itemAt(pointScene, ui->graphicsView->transform());
    if (item != nullptr)
    {
        QPointF pointItem = item->mapFromScene(pointScene);
        labelItemCord->setText(QString::asprintf("Item坐标：%.0f, %.0f",
                               pointItem.x(), pointItem.y()));
    }

}

void MainWindow::initUI()
{
   QRect rect(-200, -100, 400, 200);
   scene = new QGraphicsScene(rect);
   ui->graphicsView->setScene(scene);
   QGraphicsRectItem *item = new QGraphicsRectItem(rect);
   item->setFlags(QGraphicsItem::ItemIsFocusable
                  | QGraphicsItem::ItemIsSelectable
                  | QGraphicsItem::ItemIsMovable);

   QPen pen;
   pen.setWidth(2);
   item->setPen(pen);
   scene->addItem(item);

   //item->show();

   int ellip_width = rect.width() / 2;
   int ellip_height = rect.height() / 2;
   MyGraphicsEllipseItem *itemEllip = new MyGraphicsEllipseItem(-ellip_width/2,
                                                                -ellip_height/2,
                                                                ellip_width,
                                                                ellip_height);
   itemEllip->setBrush(QBrush(Qt::blue));
   itemEllip->setFlags(QGraphicsItem::ItemIsSelectable
                       | QGraphicsItem::ItemIsMovable
                       | QGraphicsItem::ItemIsFocusable);
   scene->addItem(itemEllip);

   MyGraphicsEllipseItem *itemCircel = new MyGraphicsEllipseItem(-ellip_width/2,
                                                                -ellip_width/2,
                                                                ellip_width,
                                                                ellip_width);
   //itemCircel->setPos(0,0);
   itemCircel->setPos(rect.right(), rect.bottom());
   itemCircel->setBrush(QBrush(Qt::red));
   itemCircel->setFlags(QGraphicsItem::ItemIsSelectable
                       | QGraphicsItem::ItemIsMovable
                       | QGraphicsItem::ItemIsFocusable);
   scene->addItem(itemCircel);


}
